package com.example.wenhua.service.impl;

import com.example.wenhua.entity.Student;
import com.example.wenhua.dto.StudentCreditsSummary;
import com.example.wenhua.mapper.StudentMapper;
import com.example.wenhua.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 学生业务逻辑实现类
 */
@Service
public class StudentServiceImpl implements StudentService {
    
    @Autowired
    private StudentMapper studentMapper;
    
    @Override
    public List<Student> getAllStudents() {
        return studentMapper.findAll();
    }
    
    @Override
    public Student getStudentBySno(String sno) {
        return studentMapper.findBySno(sno);
    }
    
    @Override
    public List<Student> getStudentsByDept(String dept) {
        return studentMapper.findByDept(dept);
    }
    
    @Override
    @Transactional
    public boolean addStudent(Student student) {
        // 检查学号是否已存在
        if (studentMapper.findBySno(student.getSno()) != null) {
            throw new RuntimeException("学号已存在：" + student.getSno());
        }
        return studentMapper.insert(student) > 0;
    }
    
    @Override
    @Transactional
    public boolean updateStudent(Student student) {
        return studentMapper.update(student) > 0;
    }
    
    @Override
    @Transactional
    public boolean deleteStudent(String sno) {
        return studentMapper.delete(sno) > 0;
    }
    
    @Override
    @Transactional
    public boolean batchImportStudents(List<Student> students) {
        if (students == null || students.isEmpty()) {
            return false;
        }
        
        // 检查是否有重复学号
        for (Student student : students) {
            if (studentMapper.findBySno(student.getSno()) != null) {
                throw new RuntimeException("学号已存在：" + student.getSno());
            }
        }
        
        return studentMapper.batchInsert(students) > 0;
    }
    
    @Override
    public Object queryStudents(com.example.wenhua.dto.StudentQueryRequest request) {
        // 计算分页偏移量
        int offset = (request.getPageNum() - 1) * request.getPageSize();
        
        // 构建查询结果
        java.util.List<Student> students = studentMapper.queryStudents(
            request.getSno(), request.getSname(), request.getSsex(), request.getSdept(),
            request.getMinAge(), request.getMaxAge(), request.getPageSize(), offset
        );
        int total = studentMapper.countStudents(
            request.getSno(), request.getSname(), request.getSsex(), request.getSdept(),
            request.getMinAge(), request.getMaxAge()
        );
        
        // 构建分页结果
        java.util.Map<String, Object> result = new java.util.HashMap<>();
        result.put("records", students);
        result.put("total", total);
        result.put("current", request.getPageNum());
        result.put("size", request.getPageSize());
        
        return result;
    }
    
    @Override
    public Integer getTotalCreditsBySno(String sno) {
        return studentMapper.getTotalCreditsBySno(sno);
    }
    
    @Override
    public List<StudentCreditsSummary> getStudentCreditsSummary() {
        return studentMapper.findStudentCreditsSummary();
    }
}